<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="是否上架" prop="videoPutaway">
        <el-select v-model="queryParams.videoPutaway" placeholder="请选择" clearable size="small">
          <el-option label="请选择" value="" />
          <el-option label="已上架" value="0" />
          <el-option label="未上架" value="1" />


        </el-select>
      </el-form-item>

      <el-form-item label="视频分类" prop="videoCategoryId">
        <el-select v-model="queryParams.videoCategoryId" clearable filterable placeholder="请选择">
          <el-option label="请选择" value="" />
          <el-option label="3D" value="9" />
          <el-option label="无码" value="10" />
          <el-option label="有码" value="11" />
        </el-select>
      </el-form-item>

      <el-form-item label="上传时间" prop="videoTime">
        <el-date-picker clearable size="small"
                        v-model="queryParams.videoTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择上传时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['xqsp:video:add']"
        >新增</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['xqsp:video:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['xqsp:video:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['xqsp:video:export']"
        >导出</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-bottom"
          size="mini"
          @click="handleTime"

        >排序</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-orange"
          size="mini"
          @click="handleRand"

        >乱序</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="videoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="65" align="center" />
      <el-table-column label="视频编号"  width="55" align="center" prop="spVideo.videoId" />
      <el-table-column label="视频名称" align="center" prop="spVideo.videoName" />
      <el-table-column label="视频作者" align="center" prop="spVideo.videoAuthor" />
      <el-table-column label="视频封面"   width="100" align="center" prop="spVideo.videoCoverImg">
        <template slot-scope="scope">
          <el-image :src="scope.row.spVideo.videoCoverImg"  :preview-src-list="[scope.row.spVideo.videoCoverImg]" style="width: 100px;height: 100px"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="演员名称" align="center" prop="actorName" />-->
      <el-table-column label="视频介绍" :show-overflow-tooltip="true" align="center" prop="spVideo.videoIntroduce" />
      <el-table-column label="观看人数" align="center" prop="spVideo.videoWatchNumber" />
      <el-table-column label="点赞人数" align="center" prop="spVideo.videoLikeNumber" />
      <el-table-column label="差评人数" align="center" prop="spVideo.videoUnlikeNumber" />
      <el-table-column label="收藏人数" align="center" prop="spVideo.videoCollectNumber" />
      <el-table-column label="付费设置" align="center" prop="spVideo.videoUnlock"  >

      </el-table-column>

      <el-table-column label="视频分类" align="center" prop="videoCategoryName" />
      <el-table-column label="是否上架" align="center" key="spVideo.videoPutaway">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.spVideo.videoPutaway"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-value="0"
            inactive-value="1"
            @change="handleStatusChange(scope.row)">

          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="清晰度设置" align="center" prop="spVideo.videoDefinition" />
      <el-table-column label="解锁所需金币数" align="center" prop="spVideo.videoUnlockGlod" />
      <el-table-column label="上传时间" align="center" prop="spVideo.videoTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.spVideo.videoTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="视频时长" align="center" prop="spVideo.videoDuration" />
      <el-table-column label="视频地址" align="center" prop="spVideo.videoUrl">

      </el-table-column>
      <el-table-column label="视频预览" align="center" prop="spVideo.videoUrlPreview" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['xqsp:video:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['xqsp:video:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-comment"
            @click="handleEvaluation(scope.row)"
          >评价</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改视频管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="视频名称" prop="videoName">
          <el-input v-model="form.videoName" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="视频作者" prop="videoAuthor">
          <el-input v-model="form.videoAuthor" placeholder="请输入视频作者" />
        </el-form-item>

        <el-form-item label="视频封面" prop="videoCoverImg" >
          <el-input v-model="form.videoCoverImg" type="textarea" placeholder="请输入内容" />

        </el-form-item>
<!--        <el-form-item label="演员" prop="actorId">-->
<!--          &lt;!&ndash;          <el-input v-model="form.actorId" placeholder="请输入演员id" />&ndash;&gt;-->
<!--          <el-select v-model="form.actorId" clearable filterable placeholder="请选择">-->
<!--            <el-option-->
<!--              v-for="item in options"-->
<!--              :key="item.actorId"-->
<!--              :label="item.actorName"-->
<!--              :value="item.actorId">-->
<!--            </el-option>-->
<!--          </el-select>-->
<!--        </el-form-item>-->
        <el-form-item label="视频介绍" prop="videoIntroduce">
          <el-input v-model="form.videoIntroduce" type="textarea"  placeholder="请输入视频介绍(字数最多500字)" maxlength="500" show-word-limit  :autosize="{minRows:4,maxRows:10}" />
        </el-form-item>
        <el-form-item label="观看人数" prop="videoWatchNumber">
          <el-input v-model="form.videoWatchNumber" placeholder="请输入观看人数(默认为0)" />
        </el-form-item>
        <el-form-item label="点赞人数" prop="videoLikeNumber">
          <el-input v-model="form.videoLikeNumber" placeholder="请输入点赞人数(默认为0)" />
        </el-form-item>
        <el-form-item label="差评人数" prop="videoUnlikeNumber">
          <el-input v-model="form.videoUnlikeNumber" placeholder="请输入差评人数(默认为0)" />
        </el-form-item>
        <el-form-item label="收藏人数" prop="videoCollectNumber">
          <el-input v-model="form.videoCollectNumber" placeholder="请输入收藏人数(默认为0)" />
        </el-form-item>
        <el-form-item label="付费设置" prop="videoUnlock" >

          <el-radio-group v-model="form.videoUnlock">

            <el-radio :label="0" >免费</el-radio>
            <el-radio :label="1"  >金币付费</el-radio>
            <el-radio :label="2" >会员免费</el-radio>

          </el-radio-group>
        </el-form-item>
        <el-form-item label="视频分类" prop="videoCategoryId">
          <!--          <el-input v-model="form.videoCategoryId" placeholder="请输入视频分类id" />-->
          <el-select v-model="form.videoCategoryId" clearable filterable placeholder="请选择">
            <el-option label="请选择" value="" />
            <el-option label="3D" :value="9" />
            <el-option label="无码" :value="10" />
            <el-option label="有码" :value="11" />
          </el-select>
        </el-form-item>

        <el-form-item label="清晰度设置" prop="videoDefinition">

          <el-select v-model="form.videoDefinition" clearable filterable placeholder="请选择清晰度">
            <el-option label="请选择清晰度" value="" />
            <el-option label="标清" value="0" />
            <el-option label="高清" value="1" />
          </el-select>
        </el-form-item>
        <el-form-item label="解锁所需金币数" prop="videoUnlockGlod">
          <el-input v-model="form.videoUnlockGlod" placeholder="请输入解锁所需金币数(默认为1)" />
        </el-form-item>
<!--        <el-form-item label="上传时间" prop="videoTime">-->
<!--          <el-date-picker clearable size="small"-->
<!--                          v-model="form.videoTime"-->
<!--                          type="datetime"-->
<!--                          value-format="yyyy-MM-dd HH:mm:ss"-->
<!--                          placeholder="选择上传时间">-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->
        <el-form-item label="视频时长" prop="videoDuration">
          <el-input v-model="form.videoDuration" placeholder="请输入视频时长" />
        </el-form-item>
        <el-form-item label="视频地址" prop="videoUrl">
          <el-input v-model="form.videoUrl" type="textarea" placeholder="请输入内容" />

        </el-form-item>
        <el-form-item label="视频预览" prop="videoUrlPreview">
          <el-input v-model="form.videoUrlPreview" type="textarea" placeholder="请输入内容" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <el-dialog :title="title" :visible.sync="open2" width="700px" append-to-body>
      <el-table v-loading="loading" :data="evaluationList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label=" 视频评价id" align="center" prop="spVideoEvaluation.videoEvaluationId" />
        <el-table-column label="视频名称" align="center" prop="videoName" />
        <el-table-column label="用户名称" align="center" prop="userName" />
        <el-table-column label="评价内容" align="center" prop="spVideoEvaluation.videoEvaluationContent" />
        <el-table-column label="评价时间" align="center" prop="spVideoEvaluation.videoEvaluationTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.videoEvaluationTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleVideoEvaluationDelete(scope.row)"
              v-hasPermi="['xqsp:evaluation:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total1>0"
        :total="total1"
        :page.sync="queryParams1.pageNum"
        :limit.sync="queryParams1.pageSize"
        @pagination="handleEvaluation(1)"
      />
      <div slot="footer" class="dialog-footer">

        <el-button @click="cancel2">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
  import { listVideo1, getVideo,uploadVideo, delVideo, addVideo1, updateVideo, exportVideo ,changeUserStatus } from "@/api/xqsp/video";
  import { listActor } from "@/api/xqsp/actor";
  import { getEvaluationByVideoId} from "@/api/xqsp/evaluation";


  export default {
    name: "Video",
    components: {


    },
    data() {
      return {



//演员下拉框


        value:'',
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        total1:0,
        // 视频管理表格数据
        videoList: [],
//评价
        evaluationList:[],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        open2:false,
        //是否显示回显快
        isShow:false,
        //视频地址
        videoUrl:"",
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          videoName: null,
          videoCoverImg: null,
          actorId: null,
          videoIntroduce: null,
          videoWatchNumber: null,
          videoLikeNumber: null,
          videoUnlock: null,
          videoCategoryId: null,
          videoPutaway: null,
          videoDefinition: null,
          videoUnlockGlod: null,
          videoTime: null,
          videoUrl: null
        },
        // 查询参数
        queryParams1: {
          pageNum: 1,
          pageSize: 10,
          videoId:null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          videoWatchNumber:[
            {
              pattern: /^[+]{0,1}(\d+)$/,
              message: '请输入正整数',
              trigger: 'blur'
            }
          ],
          videoLikeNumber:[
            {
              pattern: /^[+]{0,1}(\d+)$/,
              message: '请输入正整数',
              trigger: 'blur'
            }
          ],
          videoUnlikeNumber:[
            {
              pattern: /^[+]{0,1}(\d+)$/,
              message: '请输入正整数',
              trigger: 'blur'
            }
          ],
          videoCollectNumber:[
            {
              pattern: /^[+]{0,1}(\d+)$/,
              message: '请输入正整数',
              trigger: 'blur'
            }
          ],
          videoUnlock: [
            {required: true, message: "付费设置不能为空", trigger: "blur"}
          ],
          videoName: [
            {required: true, message: "视频名称不能为空", trigger: "blur"}
          ],
          videoCoverImg: [
            {required: true, message: "视频封面不能为空", trigger: "blur"}
          ],
          actorId: [
            {required: true, message: "演员不能为空", trigger: "blur"}
          ],
          videoIntroduce: [
            {required: true, message: "视频介绍不能为空", trigger: "blur"}
          ],
          videoCategoryId: [
            {required: true, message: "视频分类不能为空", trigger: "blur"}
          ],
          videoDefinition: [
            {required: true, message: "清晰度设置不能为空", trigger: "blur"}
          ],
          videoUnlockGlod: [

            {
              pattern: /^[+]{0,1}(\d+)$/,
              message: '请输入正整数',
              trigger: 'blur'
            }
          ],
          videoTime: [
            {required: true, message: "上传时间不能为空", trigger: "blur"}
          ],
          videoUrl: [
            {required: true, message: "视频地址不能为空", trigger: "blur"}
          ],


        }
      };
    },
    created() {
      this.getList();

    },
    methods: {


//视频播放
      look(row){
        this.videoUrl=row.spVideo.videoUrl;
        this.dialogVisible=true;
      },
      getVideo(event) {
        this.videoInfo.allVideo = event
      },
      deleteVideo(index) {
        const video = this.videoInfo.allVideo[index]
        if (video.videoLink) {
          this.videoInfo.deleteVideo.push(video)
        }
        this.videoInfo.allVideo.splice(index, 1)
      },

      /** 根据时间排序*/
      handleTime(){
        this.getSortList();
      },

      /** 根据时间排序查询 */
      getSortList() {
        this.loading = true;
        listVideo1(this.queryParams).then(response => {

          this.videoList = response.rows.sort(
            function(a,b){
              // var x=a.spVideo.videoTime.replace(/:/g,"").replace(/-/g,"").replace("","")
              // var y=b.spVideo.videoTime.replace(/:/g,"").replace(/-/g,"").replace("","")
              // return ((x>y)?-1:((x<y)?1:0));  //由大到小进行排序
              return Date.parse(b.spVideo.videoTime) - Date.parse(a.spVideo.videoTime);//时间倒序
            }
          );
          this.videoList.forEach(item=>{
            if (item.spVideo.videoUnlock==0){
              item.spVideo.videoUnlock="免费"
            }else if (item.spVideo.videoUnlock==1){
              item.spVideo.videoUnlock="金币付费"
            }else if (item.spVideo.videoUnlock==2){
              item.spVideo.videoUnlock="会员免费"
            }
            if (item.spVideo.videoDefinition==0){
              item.spVideo.videoDefinition="标清"
            }else if (item.spVideo.videoDefinition==1){
              item.spVideo.videoDefinition="高清"
            }

          });
          this.total = response.total;
          this.loading = false;
        });
      },
      /** 随机排序*/
      handleRand(){
        this.getRandList();
      },
      /** 随机排序 */
      getRandList() {
        this.loading = true;
        listVideo1(this.queryParams).then(response => {

          this.videoList = response.rows.sort(()=>{
            return Math.random()-0.5;
          });
          this.videoList.forEach(item=>{
            if (item.spVideo.videoUnlock==0){
              item.spVideo.videoUnlock="免费"
            }else if (item.spVideo.videoUnlock==1){
              item.spVideo.videoUnlock="金币付费"
            }else if (item.spVideo.videoUnlock==2){
              item.spVideo.videoUnlock="会员免费"
            }
            if (item.spVideo.videoDefinition==0){
              item.spVideo.videoDefinition="标清"
            }else if (item.spVideo.videoDefinition==1){
              item.spVideo.videoDefinition="高清"
            }
          });
          this.total = response.total;
          this.loading = false;
        });
      },

      /** 查询视频管理列表 */
      getList() {
        this.loading = true;
        listVideo1(this.queryParams).then(response => {
          this.videoList = response.rows
          this.videoList.forEach(item=>{
            if (item.spVideo.videoUnlock==0){
              item.spVideo.videoUnlock="免费"
            }else if (item.spVideo.videoUnlock==1){
              item.spVideo.videoUnlock="金币付费"
            }else if (item.spVideo.videoUnlock==2){
              item.spVideo.videoUnlock="会员免费"
            }
            if (item.spVideo.videoDefinition==0){
              item.spVideo.videoDefinition="标清"
            }else if (item.spVideo.videoDefinition==1){
              item.spVideo.videoDefinition="高清"
            }
          });
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 取消按钮
      cancel2() {
        this.open2 = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          videoId: null,
          videoName: null,
          videoCoverImg: null,
          actorId: null,
          videoIntroduce: null,
          videoWatchNumber: null,
          videoLikeNumber: null,
          videoUnlock: null,
          videoCategoryId: null,
          videoPutaway: null,
          videoDefinition: null,
          videoUnlockGlod: null,
          videoTime: null,
          videoUrl: null,
          videoAuthor:null,
          videoUrlPreview: null,
          videoDuration: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.spVideo.videoId)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加视频管理";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const videoId = row.spVideo.videoId || this.ids
        getVideo(videoId).then(response => {
          this.form = response.data;

          this.form.videoUnlock=parseInt(response.data.videoUnlock);

          this.open = true;
          this.title = "修改视频管理";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {

            if (this.form.videoId!=null){
              updateVideo(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
                this.$refs.videoChild.clearVideoLst()
              });

            }else {
              addVideo1(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();

                this.$refs.videoChild.clearVideoLst()
              });
            }
            return Promise.all(uploadList)

          }
        });
      },

      /** 查看视频评价*/

      handleEvaluation(row){
        if (row!=1){
          this.videoId=row.spVideo.videoId;
        }
        this.queryParams1.videoId=this.videoId;
        getEvaluationByVideoId(this.queryParams1).then(response => {

          this.evaluationList = response.rows;
          this.total1 = response.total;
          this.open2 = true;
          this.title =  "查看视频“"+this.videoId+"”的评价";

          this.loading = false;

        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        let tmpIds=[]
        if (this.ids.length>0){
          for (const id of this.ids) {
            tmpIds.push(id);
          }
        }else {
          tmpIds.push(row.spVideo.videoId);
        }
        this.$confirm('是否确认删除视频管理编号为"' + tmpIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delVideo(tmpIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有视频管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportVideo(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
      },
      // 是否上架
      handleStatusChange(row) {
        let text = row.spVideo.videoPutaway === "0" ? "上架" : "下架";
        this.$confirm('确认要"' + text + '""' + row.spVideo.videoName + '"视频吗?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return changeUserStatus(row.spVideo.videoId, row.spVideo.videoPutaway);
        }).then(() => {
          this.msgSuccess(text + "成功");
        }).catch(function() {
          row.spVideo.videoPutaway = row.spVideo.videoPutaway === "0" ? "1" : "0";
        });
      },



      /** 删除评价按钮操作 */
      handleVideoEvaluationDelete(row) {
        const videoEvaluationIds = row.spVideoEvaluation.videoEvaluationId;
        this.$confirm('是否确认删除视频评价编号为"' + videoEvaluationIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delEvaluation(videoEvaluationIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },







    }
  };
</script>
